package com.mineplatform.service;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.mineplatform.dao.M_MineDao;
import com.mineplatform.dao.M_TokenDao;
import com.mineplatform.dao.M_UserDao;
import com.mineplatform.domain.M_Mine;
import com.mineplatform.domain.M_Token;
import com.mineplatform.domain.M_User;
import com.mineplatform.exception.MineIdNullOrEmptyException;
import com.mineplatform.exception.MineNotExistException;
import com.mineplatform.exception.PermissionDeniedException;
import com.mineplatform.exception.TokenDisableException;
import com.mineplatform.exception.TokenNotExistException;
import com.mineplatform.exception.TokenNullOrEmptyException;
import com.mineplatform.exception.UserNotExistException;
import com.mineplatform.iservice.IM_MineService;
import com.mineplatform.util.CONSTANT;

@Service
public class M_MineService implements IM_MineService {

	private M_MineDao _m_minedao;
	private M_UserDao _m_userdao;
	private M_TokenDao _m_tokendao;

	@Autowired
	public M_MineService(M_UserDao m_userdao, M_TokenDao m_tokendao, M_MineDao m_minedao) {
		this._m_minedao = m_minedao;
		this._m_userdao = m_userdao;
		this._m_tokendao = m_tokendao;
	}

	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public List<M_Mine> find(String minecategoryid,String keywords, String provinceid, Integer top, Boolean hot, String userid) {
		// TODO Auto-generated method stub
		return this._m_minedao.find(minecategoryid,keywords, provinceid, top, hot, userid);
	}

	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public M_Mine find(String mineid) throws MineIdNullOrEmptyException, MineNotExistException {
		// TODO Auto-generated method stub
		if ((mineid == null) || (mineid.equals(""))) {

			throw new MineIdNullOrEmptyException();
		}

		M_Mine mine = this._m_minedao.findById(mineid);

		if (mine == null) {
			throw new MineNotExistException();
		}

		return mine;
	}

	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public String save(M_Mine mine) {
		// TODO Auto-generated method stub
		String id = UUID.randomUUID().toString();
		mine.setId(id);

		if (mine.getMineImages() == null) {
			mine.setMineImages("");
		}

		mine.setPraiseCount(0);
		mine.setReplyCount(0);

		this._m_minedao.save(mine);
		return id;
	}

	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public void save(M_Mine mine, String token) throws TokenNullOrEmptyException, TokenNotExistException,
			TokenDisableException, UserNotExistException, PermissionDeniedException {
		// TODO Auto-generated method stub

		if ((token == null) || (token.equals(""))) {
			throw new TokenNullOrEmptyException();
		}

		M_Token tokenModel = this._m_tokendao.findByToken(token);
		if (tokenModel == null) {
			throw new TokenNotExistException();
		}

		if (!tokenModel.getEnable()) {
			throw new TokenDisableException();
		}
		M_User user = this._m_userdao.findById(tokenModel.getUserId());
		if (user == null) {
			throw new UserNotExistException();
		}
		if (user.getUserType() != Integer.valueOf(CONSTANT.USERTYPE_DEFINE.ENTERPRISE)) {

			throw new PermissionDeniedException();
		}
		mine.setUserId(user.getId());
		mine.setCompanyName(user.getUserCompany());
		this.save(mine);
	}

}
